package com.ws.q1.controller;

import com.ws.q1.bean.User;
import com.ws.q1.conifg.Res;
import com.ws.q1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/denglu")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String name, String pass, HttpServletRequest request) {
        String s = DigestUtils.md5DigestAsHex(pass.getBytes());
        System.out.println(name + ":" + pass);
        User user = userService.login(name, s);
        request.getSession().setAttribute("user", user);
        if (user != null) {
            return "redirect:/qiantai/index";
        } else {
            return "denglu/login";
        }
    }


    // 注册
    @RequestMapping("/zhuce")
    public String add(User user, MultipartFile image, HttpServletRequest request) throws IOException {
        System.out.println(user);

        // 检查用户名是否已存在
        Boolean usernameExists = userService.isUsernameExists(user.getName());
        if (usernameExists != null && usernameExists) {
            request.setAttribute("errorMessage", "用户名已存在");
            return "denglu/flie";
        }
        if (image.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + image.getOriginalFilename().split("\\.")[1];
            user.setTou(fileName);
            image.transferTo(new File(Res.fileDir + fileName));
        } else {
            user.setTou("e18aa2ce37c243aaa8cdc0d99d1c74c5.jpg");
        }

        String s = DigestUtils.md5DigestAsHex(user.getPass().getBytes());
        user.setPass(s);
        int add = userService.add(user.getName(), user.getPass(), user.getPhone(), user.getTou());
        if (add > 0) {
            return "denglu/login";
        } else {
            request.setAttribute("errorMessage", "注册失败");
            return "redirect:denglu/zhuce";
        }
    }
    @RequestMapping("/updater")
    public String update(User user, MultipartFile image, HttpServletRequest request) throws IOException {
        System.out.println(user);
        // 检查 image 是否为空
        if (image != null && image.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + image.getOriginalFilename().split("\\.")[1];
            user.setTou(fileName);
            image.transferTo(new File(Res.fileDir + fileName));
        } else {
            // 如果没有上传新头像，保持原有头像
            User existingUser = (User) request.getSession().getAttribute("user");
            if (existingUser != null) {
                user.setTou(existingUser.getTou());
            }
        }

        // 获取用户 ID
        Integer userId = user.getId(); // 确保 user 对象中包含 id 属性

        // 检查密码是否为空，如果不为空则进行加密
        if (user.getPass() != null && !user.getPass().isEmpty()) {
            String encryptedPass = DigestUtils.md5DigestAsHex(user.getPass().getBytes());
            user.setPass(encryptedPass);
        } else {
            // 如果密码为空，保持原有密码
            User existingUser = (User) request.getSession().getAttribute("user");
            if (existingUser != null) {
                user.setPass(existingUser.getPass());
            }
        }

        // 更新用户信息
        userService.xiugai(userId, user);

        // 将更新后的用户信息重新存储到会话中
        request.getSession().setAttribute("user", user);

        // 重定向到 gerenxinxi.jsp 页面
        return "redirect:/qiantai/gerenxinxi";
    }






}




